package values.item
{
import core.Language;

import flash.geom.Point;
import flash.utils.Dictionary;

import framework.dao.EntryUtil;

import values.common.CostValue;
import values.common.IconValue;

public class ItemTemplateValue
{		
	[Data(type="field", mapping="@key")]
	public var key:String;
	
	[Data(type="field", mapping="@inventory")]
	public var inventory:String;
	
	[Data(type="field", mapping="@type")]
	public var type:String;
	
	public function get isPack():Boolean
	{
		return type == "pack";
	}
	
	[Data(type="field", mapping="@name")]
	public var baseNameKey:String;
	
	public function get name():String
	{
		return Language.getText(baseNameKey);
	}
	
	[Data(type="field", mapping="@desc")]
	public var baseDescKey:String;
	
	public function get desc():String
	{
		return Language.getText(baseDescKey);
	}
    
    protected var _icons:Dictionary;
    
    public function get icons():Dictionary
    {
        return _icons;
    }
    
    [Data(type="mapping", 
    		  mapping="icons.icon", 
    		  ref="values.common.IconValue",
    		  way="one_to_more",
    		  object="flash.utils.Dictionary",
              key="@key")]
    public function set icons(value:Dictionary):void
    {
        _icons = value;
    }
    
    public function getIconPathByKey(key:String):String
    {
		if(_icons[key])
		{
			return IconValue(_icons[key]).url;
		}
        
		return null;
    }
    
    protected var _drops:Array;
    
    public function get drops():Array
    {
        return _drops;
    }
    
    [Data(type="mapping", 
    		  mapping="drops.drop", 
    		  ref="values.item.ItemDropValue",
    		  way="one_to_more")]
    public function set drops(value:Array):void
    {
        _drops = value;
    }
    
    [Data(type="field", mapping="drops.@collect_point")]
    public var dropCollectPoint:Point = new Point(0, 0);
	
	[Data(type="field", mapping="@buyable")]
	public var buyable:Boolean;
	
	[Data(type="field", mapping="@usable")]
	public var usable:Boolean;
	
	[Data(type="field", mapping="@buy_to_inventory")]
	public var buyToInventory:Boolean;
	
	[Data(type="field", mapping="@buy_and_use")]
	public var buyAndUse:Boolean;
	
	[Data(type="field", mapping="@sell_coin")]
	public var sellCoin:Number = 0;
    
	[Data(type="field", mapping="@desc_type")]
	public var descType:String;
    
    [Data(type="field", mapping="@show_in_inventory")]
    public var showInInventory:Boolean = true;
	
	[Data(type="field",  mapping="cost")]
	public var baseCostDef:*;
	
	protected var _cost:CostValue;
	
	public function get cost():CostValue
	{
		if (!_cost)
		{
			_cost = EntryUtil.mapping(baseCostDef, CostValue);
			_cost.owner = this;
		}
		return _cost;
	}
	
	protected var _requirements:ItemRequirementValue;
	
	public function get requirements():ItemRequirementValue
	{
		return _requirements;
	}
	
	[Data(type="mapping", 
		   mapping="requirements", 
		   ref="values.item.ItemRequirementValue",
		   way="own")]
	public function set requirements(value:ItemRequirementValue):void
	{
		_requirements = value;
		value.owner = this;
	}
	
	protected var _assets:Dictionary; /* ItemAssetValue */
	
	public function get assets():Dictionary
	{
		return _assets;
	}
	
	[Data(type="mapping", 
		  mapping="assets.asset", 
		  ref="values.item.ItemAssetValue",
		  way="one_to_more",
		  object="flash.utils.Dictionary",
          key="@direction")]
	public function set assets(value:Dictionary):void
	{
		_assets = value;
	}
	
	public function getAssetByDirection(direction:String):ItemAssetValue
	{
		return _assets[direction];
	}
	
	public function get defaultAsset():ItemAssetValue
	{
		for each (var avo:ItemAssetValue in assets)
		{
			if (avo.isDefault)
			{
				return avo;
			}
		}
		return avo;
	}
	
	protected var _params:*;

	public function get params():*
	{
		return _params;
	}
	
	[Data(type="field", mapping="params")]
	public function set params(value:*):void
	{
		_params = value;
	}
	
	public function get hasParams():Boolean
	{
		return params == null ? false : true;
	}
	
	public function checkHasParams(key:String):Boolean
	{
		return hasParams && Object(params).hasOwnProperty(key);
	}
	
	public function get dynamicClassRef():Class
	{
		return ItemValue;
	}
}
}



